import { defineStore } from 'pinia';
 import {reqLogin , reqRegister} from '@/api/user';
import { UserState } from './types/type';
import { GET_TOKEN, SET_TOKEN ,REMOV_TOKNE} from '@/utils/token';
import { constantRoute } from '@/router/routes';
export const useUserStore:any = defineStore('User',{
    //数据存储区
    state:():UserState=>{
        return {
            token:GET_TOKEN(),
            menuRoutes:constantRoute,
            username: '',
            avatar: '',
            //存储当前用户是否包含某一个按钮
            buttons: [],
        }
    },
    //异步方法
    actions:{
       async  userLogin(data:any){
            let result:any = await reqLogin(data)
            if( result.code == 200 ){
                this.token = (result.data.token as string);
                SET_TOKEN((result.data.token as string))
            }else{
                return Promise.reject(new Error(result.data.message))
            }

        },
        
            userLoginOut(){
            this.token = "",
            this.username = "",
            this.avatar = "",
            REMOV_TOKNE();
                        
        }
    },
    getters:{

    }
})

